package logic

import (
	"bw-renting/house-rpc/house"
	"bw-renting/house-rpc/internal/svc"
	"bw-renting/house-rpc/model"
	"context"
	"errors"
	"strconv"

	"github.com/zeromicro/go-zero/core/logx"
)

type ReleaseHouseLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewReleaseHouseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ReleaseHouseLogic {
	return &ReleaseHouseLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ReleaseHouseLogic) ReleaseHouse(in *house.ReleaseHouseRequest) (*house.ReleaseHouseResponse, error) {
	// todo: add your logic here and delete this line
	h := model.House{
		UserId:  uint(in.UserId),
		AreaId:  uint(in.AreaId),
		Title:   in.Title,
		Address: in.Address,
		Acreage: uint(in.Acreage),
		Price:   uint(in.Acreage)*uint(in.Unit) + uint(in.Deposit),
		Unit:    strconv.Itoa(int(in.Unit)),
		Deposit: uint(in.Deposit),
		Images:  in.Images,
	}
	houses := model.ReleaseHouse(h)
	if houses.ID == 0 {
		return &house.ReleaseHouseResponse{}, errors.New("房屋发布失败")
	}
	hh := model.HouseFacility{
		HouseId:    houses.ID,
		FacilityId: uint(in.FacilityId),
	}
	hh = model.AddsHouseFacilityS(hh)
	if hh.ID == 0 {
		return &house.ReleaseHouseResponse{}, errors.New("房屋配置入失败")
	}
	return &house.ReleaseHouseResponse{
		HouseId: strconv.Itoa(int(houses.ID)),
	}, nil
}
